package com.szjlxh.data.repository;

import android.util.Log;

import com.szjlxh.data.entity.ImportantWeatherBean;
import com.szjlxh.data.entity.WaringWeatherBean;
import com.szjlxh.data.net.ApiConnection;
import com.szjlxh.data.net.ConstantUrl;
import com.szjlxh.data.utils.GsonObjectFormatter;
import com.szjlxh.domain.model.product.WarningInfo;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by liutao on 16/3/19.
 */
public class ProductWeatherRepository {

    public ProductWeatherRepository() {

    }

    public Observable<ImportantWeatherBean> weatherImportantWeather() {
        return Observable.create(new Observable.OnSubscribe<ImportantWeatherBean>() {
            @Override
            public void call(Subscriber<? super ImportantWeatherBean> subscriber) {
                String resString = null;
                try {
                    resString = ApiConnection.createGET(ConstantUrl.PRODUCT_IMPORTANT_WATHER).requestSyncCall();
                    resString = resString.substring(1, resString.lastIndexOf("]"));
                    Log.i("test", resString);
                    subscriber.onNext(new GsonObjectFormatter<ImportantWeatherBean>(resString).getObjectFromType(ImportantWeatherBean.class));
                } catch (Exception e) {
                    e.printStackTrace();
                    subscriber.onError(e.getCause());
                }

            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<List<WarningInfo>> weatherWarningWeather() {
        return Observable.create(new Observable.OnSubscribe<WaringWeatherBean>() {
            @Override
            public void call(Subscriber<? super WaringWeatherBean> subscriber) {
                String resString = null;
                try {
                    resString = ApiConnection.createGET(ConstantUrl.REQUEST_WARNING_URL).requestSyncCall();
                    subscriber.onNext(new GsonObjectFormatter<WaringWeatherBean>(resString).getObjectFromType(WaringWeatherBean.class));
                } catch (Exception e) {
                    e.printStackTrace();
                    subscriber.onError(e.getCause());
                }

            }
        }).map(new Func1<WaringWeatherBean, List<WarningInfo>>() {
            @Override
            public List<WarningInfo> call(WaringWeatherBean waringWeatherBean) {
                List<WarningInfo> warningInfos = new ArrayList<>();

                for (int i = 0; i < waringWeatherBean.getResult().size(); i++) {
                    WarningInfo temp = new WarningInfo();
                    temp.setWarninginfo("历史预警");
                    temp.setTime(waringWeatherBean.getResult().get(i).getD_UPDATETIME());
                    temp.setResString(waringWeatherBean.getResult().get(i).getVC_FILECLASS());
                    temp.setPath(waringWeatherBean.getResult().get(i).getVC_PATH());
                    warningInfos.add(temp);
                }
                return warningInfos;
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

}
